package visual;

import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import model.Descripcion;
import model.Juego;
import model.OrdenDeArresto;

public class Descripciones extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] generos = {"","masculino","femenino"};
	private String[] cabellos = {"","blanco","castanio","negro"};
	private String[] senias = {"","anillo","corbataRoja","guinioOjoIzquierdo"};
	private String[] hobbys = {"","jugar ajedrez","cocinar","andarAcaballo"};
	private String[] vehiculos = {"","limosina","cupe"};
	
	private JComboBox boxSexo;
	private JComboBox boxCabello;
	private JComboBox boxSenia;
	private JComboBox boxHobby;
	private JComboBox boxVehiculo;
	
	private JLabel sexo;
	private JLabel cabello;
	private JLabel senia;
	private JLabel hobby;
	private JLabel vehiculo;
	
	private JButton buscar;
	private JButton cancelar;
	private Descripcion desc;
	private Juego juego;
	private String sSexo = "", sCabello = "", sSenia = "", sHobby = "", sVehiculo = "";
	
	private InterfazPrincipal interfazContenedora;
	
	public Descripciones(Juego juego, InterfazPrincipal ip){
		super("Descripcion");
		this.juego = juego;
		this.interfazContenedora = ip;
		desc = new Descripcion(); 
		Container pane = getContentPane();
		setBounds(800, 400, 400, 300);
        SpringLayout layout = new SpringLayout();
		setLayout(layout);
		boxSexo = new JComboBox(generos);
		boxCabello = new JComboBox(cabellos);
		boxSenia = new JComboBox(senias);
		boxHobby = new JComboBox(hobbys);
		boxVehiculo = new JComboBox(vehiculos);
		

		boxSexo.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent event){
						if(event.getStateChange()==ItemEvent.SELECTED)
							sSexo = event.getItem().toString();
							
					}
				}
			);

		boxCabello.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent event){
						if(event.getStateChange()==ItemEvent.SELECTED)
							sCabello = event.getItem().toString();
					}
				}
			);
		
		boxSenia.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent event){
						if(event.getStateChange()==ItemEvent.SELECTED)
							sSenia = event.getItem().toString();
					}
				}
			);
		
		boxHobby.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent event){
						if(event.getStateChange()==ItemEvent.SELECTED)
							sHobby = event.getItem().toString();
					}
				}
			);
		
		boxVehiculo.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent event){
						if(event.getStateChange()==ItemEvent.SELECTED)
							sVehiculo = event.getItem().toString();
					}
				}
			);
		
		sexo = new JLabel("Sexo: ");
		cabello = new JLabel("Cabello: ");
		senia = new JLabel("Senia: ");
		hobby = new JLabel("Hobby: ");
		vehiculo = new JLabel("Vehiculo: ");
		
//campo Sexo
		layout.putConstraint(SpringLayout.WEST, sexo, 5, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.NORTH, sexo, 5,  SpringLayout.NORTH, pane);
		layout.putConstraint(SpringLayout.WEST, boxSexo, 5, SpringLayout.EAST, sexo);
		layout.putConstraint(SpringLayout.NORTH, boxSexo, 5, SpringLayout.NORTH, pane);

//campo Cabello	
		layout.putConstraint(SpringLayout.WEST, cabello, 5, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.NORTH, cabello, 40,  SpringLayout.NORTH, pane);
		layout.putConstraint(SpringLayout.WEST, boxCabello, 5, SpringLayout.EAST, cabello);
		layout.putConstraint(SpringLayout.NORTH, boxCabello, 35, SpringLayout.NORTH, pane);
//campo Senia
		layout.putConstraint(SpringLayout.WEST, senia, 5, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.NORTH, senia, 70,  SpringLayout.NORTH, pane);
		layout.putConstraint(SpringLayout.WEST, boxSenia, 5, SpringLayout.EAST, senia);
		layout.putConstraint(SpringLayout.NORTH, boxSenia, 65, SpringLayout.NORTH, pane);
//campo Hobby
		layout.putConstraint(SpringLayout.WEST, hobby, 5, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.NORTH, hobby, 100,  SpringLayout.NORTH, pane);
		layout.putConstraint(SpringLayout.WEST, boxHobby, 5, SpringLayout.EAST, hobby);
		layout.putConstraint(SpringLayout.NORTH, boxHobby, 95, SpringLayout.NORTH, pane);
//campo Vehiculo
		layout.putConstraint(SpringLayout.WEST, vehiculo, 5, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.NORTH, vehiculo, 130,  SpringLayout.NORTH, pane);
		layout.putConstraint(SpringLayout.WEST, boxVehiculo, 5, SpringLayout.EAST, vehiculo);
		layout.putConstraint(SpringLayout.NORTH, boxVehiculo, 125, SpringLayout.NORTH, pane);
		
		pane.add(sexo);	pane.add(cabello); pane.add(senia); pane.add(hobby); pane.add(vehiculo);
		pane.add(boxSexo); pane.add(boxCabello); pane.add(boxSenia); pane.add(boxHobby); pane.add(boxVehiculo);

//Handler
		Handler handler = new Handler();
//Buscar
		buscar = new JButton("BUSCAR");
		buscar.addActionListener(handler);
//Cancelar
		cancelar = new JButton("VOLVER");
		cancelar.addActionListener(handler);
		
		layout.putConstraint(SpringLayout.WEST, buscar, 100, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.SOUTH, buscar, -40,  SpringLayout.SOUTH, pane);
		pane.add(buscar);
		
		layout.putConstraint(SpringLayout.WEST, cancelar, 250, SpringLayout.WEST, pane);
		layout.putConstraint(SpringLayout.SOUTH, cancelar, -40,  SpringLayout.SOUTH, pane);
		pane.add(cancelar);
	
	}
	
	private class Handler implements ActionListener {
		
		public void actionPerformed(ActionEvent event){
					
			if(event.getSource()== buscar){
				desc.setSexo(sSexo);
				desc.setCabello(sCabello);		
				desc.setSenia(sSenia); 		
				desc.setVehiculo(sVehiculo);		
				desc.setHobby(sHobby); 						
				juego.setDescripcionJugador(desc);
				
				juego.emitirOrdenParaJugador();
				OrdenDeArresto condicion =	juego.obtenerJefatura().emitirOrden(juego.obtenerJugador().obtenerDescripcionPosibleLadron());
				if(condicion != null){ JOptionPane.showMessageDialog(null, "Orden de arresto para: "+condicion.getNombre()+ "."); }
				else{ JOptionPane.showMessageDialog(null, "Multiples sospechosos con esa descripcion. Orden de arresto no emitida."); }
				interfazContenedora.actualizarInterfaz();
			}
			else{ if(event.getSource()== cancelar)	
				interfazContenedora.actualizarInterfaz();
				dispose();
			}
		}
	}
	
}